/**
 * 				MochitoDataHandlerFactory.java
 *				------------
 *		
 *   Author:	bin04017@gmail.com
 *	 Created:	22.12.2006
 *	 Project:	Mochito-TransferFramework
 *	 Package:	mochitoDataHandlerFactory
 *	 Usage:		get data handler for mochito system
 * *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */
package mochito.factory;

import java.net.InetAddress;
import java.util.HashMap;

import mochito.MochitoException;
import mochito.clientSide.MochitoSockedClient;
import mochito.serverSide.MochitoSocketServerCreator;
import mochito.tranferFactory.MochitoHost;
import mochito.tranferFactory.MochitoTransferFactory;

/**
 * @author bin04017
 * 
 */
public class MochitoFactory {

	private static MochitoFactory instance = null;
	
	private MochitoTransferFactory mochitoTransferFactory = null;

	private HashMap<MochitoHost, MochitoSocketServerCreator> opendServers = new HashMap<MochitoHost, MochitoSocketServerCreator>();
	/**
	 * Consturctor
	 * @throws MochitoException 
	 * 
	 */
	private MochitoFactory() throws MochitoException {
		init();
	}
	/**
	 * 
	 * @return the MochitoDataHandlerFactory
	 * @throws MochitoException 
	 */
	public static MochitoFactory getInstance() throws MochitoException {
		if (instance == null) {
			instance = new MochitoFactory();
		}
		return instance;
	}

	private void init() throws MochitoException{
		//set mochitoTransferFactory
		if (mochitoTransferFactory == null) {
			mochitoTransferFactory = MochitoTransferFactory.getInstance();			
		}
	}
	//----------------------------- getConnection ---------------------------------
	public MochitoSockedClient getConnection(InetAddress host, Integer port) throws MochitoException{
		return mochitoTransferFactory.getConnection(new MochitoHost(host, port));
	}
	
	public MochitoSockedClient getConnection(String host, Integer port) throws MochitoException{
		return mochitoTransferFactory.getConnection(new MochitoHost(host, port));
	}
	
	public MochitoSockedClient getConnection(InetAddress host) throws MochitoException{
		return mochitoTransferFactory.getConnection(new MochitoHost(host));
	}
	
	public MochitoSockedClient getConnection(String host) throws MochitoException{
		return mochitoTransferFactory.getConnection(new MochitoHost(host));
	}
	
	public MochitoSockedClient getConnection(MochitoHost host) throws MochitoException{
		return mochitoTransferFactory.getConnection(host);
	}
	
	public MochitoSockedClient getConnection(Integer port) throws MochitoException{
		return mochitoTransferFactory.getConnection(new MochitoHost(port));
	}
	//----------------------------- create Server ---------------------------------
	public MochitoSocketServerCreator createServer() throws MochitoException{
		MochitoHost host = new MochitoHost();
		return createServer(host);
	}
	
	public MochitoSocketServerCreator createServer(Integer port) throws MochitoException{
		MochitoHost host = new MochitoHost(port);
		return createServer(host);
	}
	private MochitoSocketServerCreator createServer(MochitoHost host){
		if(opendServers.containsKey(host)){
			return opendServers.get(host);
		}else {	
			MochitoSocketServerCreator creator = mochitoTransferFactory.createServer(host);
			opendServers.put(host, creator);
			return creator;
		}
	}
	
}
